package com.sunsharing.transport;

import com.sunsharing.common.Constant;
import com.sunsharing.common.Context;
import com.sunsharing.common.URL;
import com.sunsharing.common.config.Config;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 长连接通道 处理心跳问题
 * User: criss
 * Date: 13-4-1
 * Time: 下午11:13
 * To change this template use File | Settings | File Templates.
 */
public abstract class LongChannel extends Channel {
    /*判断是否客户端，客户端发送心跳*/
    //public boolean client = false;
    /*最后活跃时间*/
    public long lastActiveTime = new Date().getTime();

    /**是否客户端主动模式，默认是被动*/
    //public boolean byClient = false;

    ScheduledExecutorService service = null;

    static Logger logger  = Logger.getLogger(LongChannel.class);

    public  LongChannel(URL url)
    {
        this.url = (url);
    }

    public boolean isClient()
    {
        String isClient = (String)url.getIsClient();
        if(isClient==null)
        {
            throw new RuntimeException("URL里面必须要有isclient参数");
        }else
        {
            return new Boolean(isClient);
        }
    }


    protected void sendMsg(Context context)
    {
        //lastActiveTime = new Date().getTime();
    }

    public void refreshLastActiveTime()
    {
        //logger.error("刷新时间-------:"+this.url);
        lastActiveTime = new Date().getTime();
    }

    public void initMailBox()
    {
        super.initMailBox();
        if(service!=null)
        {
            service.shutdown();
            service = null;
        }
        lastActiveTime = new Date().getTime();
        service = Executors.newScheduledThreadPool(1);
        service.scheduleAtFixedRate(new HeartDect(this),0,Constant.HEART_INTERVAL, TimeUnit.SECONDS);
    }

    public void restartMailBox()
    {
        super.restartMailBox();
        lastActiveTime = new Date().getTime();
    }

    public void stop()
    {
        super.stop();

//        if(service!=null)
//        {
//            service.shutdown();
//            service = null;
//        }
    }

    public static URL initConnectURL(String msg,URL initURL,boolean client,String ip,int port)
    {
        URL returnUrl = URL.valueOf(msg);
        returnUrl.setIp(ip);

        //this.url.setParameters(url);
        //处理协议名 fromNode 需要带上协议名 同一个协议会起多个服务。
        /**
         * 服务端:fromNode:为客户端节点 TONode当前节点 端口为动态生成的端口
         * 客户端:fromNode:当前节点 TONode的服务端节点 端口为服务端开放的端口
         */
        if(client)
        {
            //logger.info("收到服务端URL:"+url);
            //returnUrl.putPar(Constant.URL_PAR_FROM_NODE_ID, returnUrl.getMyNodeId());
            returnUrl.putPar(Constant.URL_PAR_FROM_NODE_ID, initURL.getMyNodeId());
            returnUrl.putPar(Constant.URL_PAR_TO_NODE_ID,returnUrl.getMyNodeId());
            returnUrl.putPar(Constant.URL_PAR_CHANNEL_NAME,returnUrl.getChannelName());
            returnUrl.putPar(Constant.URL_PAR_MY_NODE_ID, initURL.getMyNodeId());
            returnUrl.setPort(returnUrl.getPort());
            returnUrl.putPar(Constant.URL_PAR_BYCLIENT,initURL.getByClient());
        }else
        {
            returnUrl.putPar(Constant.URL_PAR_FROM_NODE_ID,returnUrl.getMyNodeId());
            returnUrl.putPar(Constant.URL_PAR_TO_NODE_ID,initURL.getMyNodeId());

            returnUrl.putPar(Constant.URL_PAR_CHANNEL_NAME,initURL.getChannelName());
            returnUrl.putPar(Constant.URL_PAR_BYCLIENT,returnUrl.getByClient());
            returnUrl.setPort(port);
            returnUrl.putPar(Constant.URL_PAR_MY_NODE_ID, Config.getInstance().getNodeId());
        }
        //是否客户端连接
        returnUrl.putPar(Constant.URL_PAR_ISCLIENT,client+"");
        return returnUrl;
    }

    protected void finalize() throws java.lang.Throwable
    {
        super.finalize();
        if(service!=null)
        {
            service.shutdown();
            service = null;
        }

    }



}
